#include "GeoSLDReader.h"

GeoSLDReader::GeoSLDReader(QString path){
    this->path = path;
}

bool GeoSLDReader::read(GeoLayer* layer) {
    QFile file(this->path);
    file.open(QIODevice::ReadOnly | QIODevice::Text);
    QXmlStreamReader xmlReader(&file);
    GeoObject* obj = nullptr;
    GeoStyle * style = nullptr;
    while (!file.atEnd()) {
        QXmlStreamReader::TokenType nType = xmlReader.readNext();
        if (nType == QXmlStreamReader::StartElement) {
            QString strElementName = xmlReader.name().toString();
            if (strElementName == "PropertyName") {
                QString propertyName = xmlReader.readElementText();
                xmlReader.readNext();
                xmlReader.readNext();
                QString Literal = xmlReader.readElementText();

                obj = layer->getObjByProperty(propertyName, Literal);
                if (obj == nullptr) {
                    continue;
                }
                else {
                    style = new GeoStyle();
                    obj->setStyle(style);
                }
            }
            else if (strElementName == "SvgParameter") {
                QString name = xmlReader.attributes().value("name").toString();
                //填充颜色
                if (name == "fill") {
                    if (obj == nullptr)
                        continue;
                    style->fillCcolor = QColor(xmlReader.readElementText());
                }
                //线条颜色
                else if (name == "stroke") {
                    if (obj == nullptr)
                        continue;
                    style->strokeColor = QColor(xmlReader.readElementText());
                }
                //线宽
                else if (name == "stroke-width") {
                    if (obj == nullptr)
                        continue;
                    style->strokeWidth = xmlReader.readElementText().toFloat();
                }
                else {
                    //其他的先不读取
                }
            }
        }
    }
    file.close();
    return true;
}
